/******************************************************************************
 **
 **    LISA Confidential
 **
 **    Source Materials
 **
 **    (C) COPYRIGHT LISA 2008-2009
 **
 **
 **  File Name:      GetCurrentTaskInstanceForOneUserCommand.java
 **  Description:
 **  Date:           2008-10-28
 **  Author:         LisaQian
 ******************************************************************************/
package com.ti.andon.workflow.command;

import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmContext;
import org.jbpm.command.Command;
import org.jbpm.taskmgmt.exe.TaskInstance;

public class GetCurrentTaskInstanceForOneUserCmd implements Command
{
	/**
	 * Serial Version ID.
	 */
	private static final long serialVersionUID = -9017009006429726268L;

	private static Log logger = LogFactory.getLog(GetCurrentTaskInstanceForOneUserCmd.class);
	
	String businessKey;

	/**
	 * the process definition name of the current process
	 */
	String processDefinition;

	Long actorId;

	public GetCurrentTaskInstanceForOneUserCmd(String businessKey, String processDefinition, Long actorId)
	{
		this.businessKey = businessKey;
		this.processDefinition = processDefinition;
		this.actorId = actorId;
	}

	/**
	 * Retrieves the task instance matching the process definition name, business key and the actor
	 * id. If only one record found, return it.
	 * 
	 * @param jbpmContext
	 * @return the task instance matching the process definition name, business key and the actor
	 *         id. If only one record found, return it.
	 */
	public Object execute(JbpmContext jbpmContext) throws Exception
	{
		TaskInstance taskInstance = null;
		if (null == businessKey || null == actorId)
		{
			logger.error("GetCurrentTaskInstanceForOneUserCmd.execute() : Neither businessKey or actorId can be null!!!");
			return null;
		}
		try
		{
			GetCurrentTaskInstancesCmd getCurrentTaskInstancescommand = new GetCurrentTaskInstancesCmd(
					this.businessKey, this.processDefinition);
			Collection taskInstances = (Collection) getCurrentTaskInstancescommand.execute(jbpmContext);
			if (null != taskInstances)
			{
				Iterator iterTaskInstances = taskInstances.iterator();
				if (taskInstances.size() == 1)
				{
					TaskInstance tInstance = (TaskInstance) iterTaskInstances.next();
					if (null == this.actorId
							|| (null != tInstance && (Long.valueOf(tInstance
									.getActorId())).equals(this.actorId)))
					{
						taskInstance = tInstance;
					}
				}
				else
				{
					while (iterTaskInstances.hasNext())
					{
						TaskInstance temp = (TaskInstance) iterTaskInstances.next();
						String taskUser = temp.getActorId();
						if (null != taskUser && null != this.actorId && (Long.valueOf(taskUser)).equals(this.actorId))
						{
							taskInstance = temp;
							break;
						}
					}
				}
			}			
		}
		catch (Exception e)
		{
			logger.error("GetCurrentTaskInstanceForOneUserCmd.execute() : Error occurred : " + e.toString());
		}		
		return taskInstance;
	}

}
